  

  
  use RFS_master_data, clear
	 
	
	 * clients' aggregate orderflow:
	 preserve
		gen signed_q 		= bs_clients*quantity
		bys st0_date instrumentid  : egen agg_q = total(signed_q)
		duplicates drop instrumentid  st0_date, force
		cap drop tt
		egen tt = group(st0_date)
		xtset instrumentid   tt
		gen agg_q1 = F.agg_q
		gen agg_q2 = F.agg_q + F2.agg_q
		gen agg_q3 = F.agg_q + F2.agg_q + F3.agg_q
		gen agg_q4 = F.agg_q + F2.agg_q + F3.agg_q + F4.agg_q
		gen agg_q5 = F.agg_q + F2.agg_q + F3.agg_q + F4.agg_q + F5.agg_q
		keep st0_date agg_q* instrumentid
		save cbsg_flows_inst, replace
	 restore
	 
	  preserve
	  	  * drop more sophisticated
	  	 drop if activeZ  == 1

		gen signed_q 		= bs_clients*quantity
		bys st0_date instrumentid  : egen agg_q = total(signed_q)
		duplicates drop instrumentid  st0_date, force
		cap drop tt
		egen tt = group(st0_date)
		xtset instrumentid   tt
		gen Oagg_q1 = F.agg_q
		gen Oagg_q2 = F.agg_q + F2.agg_q
		gen Oagg_q3 = F.agg_q + F2.agg_q + F3.agg_q
		gen Oagg_q4 = F.agg_q + F2.agg_q + F3.agg_q + F4.agg_q
		gen Oagg_q5 = F.agg_q + F2.agg_q + F3.agg_q + F4.agg_q + F5.agg_q
		keep st0_date Oagg_q* instrumentid
		save cbsg_flows_others, replace
	 restore
	  
  	 gen signed_q 		= bs_clients*quantity
	  
	 * generate average trade size at day-client level
	 bys call st0_date instrumentid: egen asize = mean(quantity)
	 bys call st0_date instrumentid: egen aOF   = total(signed_q)
	 bys call st0_date instrumentid: egen conn   = nvals(coun)
	 bys call st0_date instrumentid: egen vols   = total(quantity)
	 
	 duplicates drop call st0_date instrumentid, force
	 
	  * keep more sophisticated
	  keep if activeZ == 1
	 
	 * merge in the aggregate data from above
	 
	 merge m:1 st0_date instrumentid using  cbsg_flows_inst 
	 
	 keep if _merge == 3
	 drop _merge
	 
	 bys call  instrumentid : egen sizeMED = median(asize) 
	 
	 gen size2 = 1
	 replace size2 = 2 if asize > sizeMED
	  
	 ** compute aggregate orderflow by order-type (small trades vs large trades)
	 cap drop aux*
	 	 bys st0_date instrumentid : egen aux_1 = total(aOF)   if size2 == 1
	 	 bys st0_date instrumentid : egen aux_2 = total(aOF)   if size2 == 2
	 	 bys st0_date instrumentid : egen aOF_1  = min(aux_1)   
	 	 bys st0_date instrumentid : egen aOF_2  = min(aux_2)   
	 	
		bys st0_date instrumentid : egen aOF_T = total(aOF)  

	 duplicates drop st0_date instrumentid, force
	  
	  
	 
	* generate controls
	gen log_conn = log(conn)
	gen log_vols = log(vols)
	
	* compute cumulate orderflow for clients
	egen tt = group(st0_date)
	xtset instrumentid   tt
	
	global vars "aOF_1 aOF_2 aOF_T "
	foreach j of global vars {
		cap drop L`j' L2`j'
		gen L`j' = L.`j'
		gen L2`j' = L2.`j'
	
		replace L`j' = 0 if L`j' == .
		replace L2`j' = 0 if L2`j' == .
		
		
     	gen `j'2   = `j' + L`j'  

		gen `j'3   = `j' + L`j' + L2`j'
	}

	 merge m:1 st0_date instrumentid using  cbsg_flows_others
	 
	 drop  if _merge == 2
	 drop _merge
	 
	 
  
   
 cap estimates drop cc*
 cap estimates drop dd*

      
			qui {
 cap drop yy* 
 cap drop xx*
 gen yy1 =  Oagg_q1
 gen yy5 =  Oagg_q5
 
 gen xxT = aOF_T   // 
 
   gen xxS = aOF_13    // 
   gen xxL = aOF_23    // 

   foreach jj of varlist xx* yy* {
      qui winsor2 `jj' , replace cuts(1 99)
   }
 
  gen xxC = 1
   reghdfe yy1 xxT , a(xxC) cluster(instrumentid st0_date)
  estimates store cc1
   reghdfe yy1 xxT , a(st0_date) cluster(instrumentid st0_date)
  estimates store cc2
   reghdfe yy1 xxT , a(instrumentid st0_date) cluster(instrumentid st0_date)
  estimates store cc3  
  
   reghdfe yy5 xxT , a(xxC) cluster(instrumentid st0_date)
  estimates store cc4
   reghdfe yy5 xxT , a(st0_date) cluster(instrumentid st0_date)
  estimates store cc5
   reghdfe yy5 xxT , a(instrumentid st0_date) cluster(instrumentid st0_date)
  estimates store cc6  
 
 
 
    reghdfe yy1  xxS xxL , a(xxC) cluster(instrumentid st0_date)
  estimates store dd1
   reghdfe yy1  xxS xxL , a(st0_date) cluster(instrumentid st0_date)
  estimates store dd2
   reghdfe yy1  xxS xxL , a(instrumentid st0_date) cluster(instrumentid st0_date)
  estimates store dd3  
  
   reghdfe yy5  xxS xxL , a(xxC) cluster(instrumentid st0_date)
  estimates store dd4
   reghdfe yy5  xxS xxL , a(st0_date) cluster(instrumentid st0_date)
  estimates store dd5
   reghdfe yy5  xxS xxL , a(instrumentid st0_date) cluster(instrumentid st0_date)
  estimates store dd6  
  
		}
	  

		   
	 esttab cc*    ///
 , star("*" 0.1 "**" 0.05 "***" 0.01) b(%10.3f) t(%10.2f) scalars(N r2) sfmt(%10.3f)   replace
  
 	 esttab dd*      ///
 , star("*" 0.1 "**" 0.05 "***" 0.01) b(%10.3f) t(%10.2f) scalars(N r2) sfmt(%10.3f)   replace
 
 		   
	 esttab cc*   using size_dongOFT.tex   ///
 , star("*" 0.1 "**" 0.05 "***" 0.01) b(%10.3f) t(%10.2f) scalars(N r2) sfmt(%10.3f)   replace
  
 	 esttab dd*   using size_dongOFsl.tex   ///
 , star("*" 0.1 "**" 0.05 "***" 0.01) b(%10.3f) t(%10.2f) scalars(N r2) sfmt(%10.3f)   replace
 
  
  
   
	
	
	
	
  
 
 
  